package com.itheima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.pojo.User;
import com.itheima.common.vo.OrderVO;
import com.itheima.common.vo.UserOrderVO;
import com.itheima.user.clients.OrderClient;
import com.itheima.user.mapper.UserMapper;
import com.itheima.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private OrderClient orderClient;

    @Autowired
    private Environment environment;

    @Override
    public User findUserByUsername(String username) {
        String property = environment.getProperty("server.port");
        System.out.println(property);

        if (username==null){
            throw new RuntimeException("参数传递错误");
        }

        User user = getOne(new QueryWrapper<User>().eq("username", username));

        return user;
    }

    @Override
    public UserOrderVO queryUserOrderVOByUsername(String username) {
        //查询user对象
        User user = findUserByUsername(username);

        //新建一个UserOrderVO对象
        UserOrderVO userOrderVO = new UserOrderVO();

        //赋值
        userOrderVO.setUsername(username);
        userOrderVO.setNickName(user.getNickName());
        userOrderVO.setHeadPic(user.getHeadPic());

        //调用Order服务,查询当前username对应的订单信息
        List<OrderVO> orderVOList = orderClient.findOrderByUsername(username);

        //赋值
        userOrderVO.setOrderList(orderVOList);

        return userOrderVO;
    }
}